
package webprocessors;

import java.io.IOException;
import java.util.LinkedList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import readers.QueryStringHandler;
import readers.UserReader;

/**
 * This returns html that lists all groups belonging to a teacher
 * @author Chris
 */
public class webprocessorGroup extends webprocessor {
    public webprocessorGroup() {}
    
    @Override
    public String process(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        QueryStringHandler QSH = new QueryStringHandler(request.getQueryString());
        String failed = QSH.getElement("failed");
        String failedMsg = "";
        if(failed != null) {
            failedMsg = "<h3>Failed to add group</h3>";
        }

        String user = getUserName(request);
        LinkedList<String> lst = new LinkedList<String>();
        
        if(!UserReader.getTeacherGroups(user, lst)) {
            return "Please add a group";
        }
        
        String grp;
        String output = failedMsg + "<table border=1><tr><td>Group</td><td>Group Access</td></tr>";   
        while(!lst.isEmpty()) {
            grp = lst.removeFirst();
            
            output += "<tr><td>" + grp + "</td><td><a href='groupAccess.jsp?grpid=" +
                    grp + "'>Access</a></td>"
                    +"</tr>";
        }
        output += "</table>";
        
        return output;
    }

}
